package model;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CongTy {
//attribute:
	private List<NhanVien> dsNhanVien;
	private double qshct;
	
	Scanner s;
	
	
//constructor: 
	public CongTy() {
		dsNhanVien = new ArrayList<NhanVien>();
		setQshct(qshct);
	}

	//nhap thong tin
	public void nhapThongTin(int n) {
		NhanVien nv;
		for(int i = 0; i < n; i++) {
			System.out.println("Nhân viên thứ " + i + ": ");
			nv = new NhanVien();
			nv.nhapThongTin();
			dsNhanVien.add(nv);
		}
	}
	
	//tinh quy sinh hoat hang thang cua cong ty
	public double tinhQSHCT() {
		double q = 0;
		for(NhanVien nv: dsNhanVien) {
			q += nv.tinhQSHNV();
		}
		return q;
	}
	
	//ham lap bao cao nhan su
	public void baoCaoNhanSu(String fileName) throws IOException {
		FileWriter fw;
		PrintWriter pw;
		
		try {
			fw = new FileWriter(fileName);
			pw = new PrintWriter(fw);
			for(int i = 0; i < dsNhanVien.size(); i++) {
				pw.print(dsNhanVien.get(i).getSCMND() + " -- ");
				pw.print(dsNhanVien.get(i).getHoTen() + " -- ");
				pw.print(dsNhanVien.get(i).getDiaChi() + " -- ");
				pw.print(dsNhanVien.get(i).getGioiTinh() + "\n");
			}
			
			fw.close();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	//ham lap bao cao ke toan cho toan cong ty (file binary)
	public void baoCaoKeToan(String fileName) throws IOException {
		DataOutputStream dos;
		FileOutputStream fos;
		
		try {
			fos = new FileOutputStream(fileName);
			dos = new DataOutputStream(fos);
			
			for(int i = 0; i < dsNhanVien.size(); i++) {
				dos.writeUTF(dsNhanVien.get(i).getSCMND());
				dos.writeDouble(dsNhanVien.get(i).getLuong());
				dos.writeDouble(dsNhanVien.get(i).tinhQSHNV());
			}
			
			dos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	//ham doc bao cao ke toan cua cong ty
	public void docBaoCaoKeToan(String fileName) throws IOException {
		DataInputStream dis;
		FileInputStream fis;
		System.out.println("Hello 1");
		try {
			System.out.println("Doc nhi phan: ");
			fis = new FileInputStream(fileName);
			dis = new DataInputStream(fis);
			for(int i = 0; i < dsNhanVien.size(); i++) {
				System.out.println("Nhân viên thứ " + i + ": ");
				String cmnd = dsNhanVien.get(i).getSCMND();
				double luong = dsNhanVien.get(i).getLuong();
				double quy = dsNhanVien.get(i).tinhQSHNV();
				
				cmnd = dis.readUTF();
				System.out.println("\tCMND: " + cmnd);
				
				luong = dis.readDouble();
				System.out.println("\tLuong: " + luong);
				
				quy = dis.readDouble();
				System.out.println("\tQuy: " +  quy);
			}
			
			fis.close();
			dis.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

//settors and gettors: 
	public List<NhanVien> getDsNhanVien() {
		return dsNhanVien;
	}

	public void setDsNhanVien(List<NhanVien> dsNhanVien) {
		this.dsNhanVien = dsNhanVien;
	}

	public double getQshct() {
		return qshct;
	}

	public void setQshct(double qshct) {
		this.qshct = qshct;
	}


}
